package leetcode.dfs;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: https://leetcode.cn/problems/generate-parentheses/
 * @Author Ammar
 * @Create 2023/7/19 19:38
 */
public class _22_括号生成 {
    public List<String> generateParenthesis(int n) {
        List<String> result = new ArrayList<>();
        if (n < 0) return result;
        char[] string = new char[n << 1];
        dfs(0, n, n, string, result);
        return result;
    }

    private void dfs(int index, int left, int right, char[] string, List<String> result) {
        if (index == string.length) {
            result.add(new String(string));
            return;
        }

        if (left > 0) {
            string[index] = '(';
            dfs(index + 1, left - 1, right, string, result);
        }

        if (right > 0 && right > left) {
            string[index] = ')';
            dfs(index + 1, left, right - 1, string, result);
        }
    }
}
